/*
 * Created by JFormDesigner on Wed Jul 08 10:07:11 CST 2020
 */
package entity.JFrame;

import entity.JPanel.UserLoginJPanel;
import entity.Model.User;
import utils.JDBCUtil;

import java.awt.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Random;
import javax.swing.*;

/**
 * @author Brainrain
 */
public class
NewUserJFrame extends JFrame {
    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JLabel label4;
    private JTextField textField1;
    private JTextField textField2;
    private JTextField textField3;
    private JButton button1;
    private JButton button2;
    private JLabel label5;
    private JLabel label6;
    private JLabel label7;
    private JLabel label8;
    //声明随机数字符串
    private String str = "";
    private String number[] = new String[4];
    private Random random = new Random();
    //验证码输入的随机数
    private String newrandom;
    //数据库参数
    private PreparedStatement preparedStatement;
    private int result;
    private ResultSet resultSet;
    private static Connection con;

    public NewUserJFrame() {
        try {
            con = JDBCUtil.getConnect();
        } catch (Exception e) {
        }
        initComponents();
        setVisible(true);
        setLocationRelativeTo(null);
        setSize(500, 430);
        setLocationRelativeTo(null);
        //设置窗口背景颜色
        getContentPane().setBackground(new Color(135, 206, 235));
        setTitle("注册");
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        label1 = new JLabel();
        label2 = new JLabel();
        label3 = new JLabel();
        label4 = new JLabel();
        textField1 = new JTextField();
        textField2 = new JTextField();
        textField3 = new JTextField();
        button1 = new JButton();
        button2 = new JButton();
        label5 = new JLabel();
        label6 = new JLabel();
        label7 = new JLabel();
        label8 = new JLabel();
        //======== this ========
        setBackground(new Color(0, 204, 204));
        Container contentPane = getContentPane();
        contentPane.setLayout(null);
        contentPane.add(label1);
        label1.setBounds(new Rectangle(new Point(85, 25), label1.getPreferredSize()));
        //---- label2 ----
        label2.setText("\u8d26\u53f7\uff1a");
        label2.setForeground(Color.blue);
        label2.setFont(new Font("\u6977\u4f53", Font.BOLD, 20));
        contentPane.add(label2);
        label2.setBounds(130, 30, 75, 55);
        //---- label3 ----
        label3.setText("\u5bc6\u7801\uff1a");
        label3.setForeground(Color.blue);
        label3.setFont(new Font("\u6977\u4f53", Font.BOLD, 20));
        contentPane.add(label3);
        label3.setBounds(130, 95, 75, 50);
        //---- label4 ----
        label4.setText("\u8bf7\u8f93\u5165\u9a8c\u8bc1\u7801\uff1a");
        label4.setFont(new Font("\u7b49\u7ebf", Font.BOLD | Font.ITALIC, 13));
        label4.setForeground(new Color(51, 0, 51));
        contentPane.add(label4);
        label4.setBounds(130, 170, 95, 50);
        contentPane.add(textField1);
        textField1.setBounds(190, 40, 155, 35);
        contentPane.add(textField2);
        textField2.setBounds(190, 105, 155, 35);
        contentPane.add(textField3);
        textField3.setBounds(220, 175, 125, 35);
        //---- button1 ----
        button1.setText("\u6ce8\u518c");
        button1.setBackground(new Color(102, 255, 102));
        contentPane.add(button1);
        button1.setBounds(135, 245, 78, 30);
        button1.addActionListener((e) -> {
            try {
                submit();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        });
        //---- button2 ----
        button2.setText("\u53d6\u6d88");
        button2.setBackground(new Color(102, 255, 102));
        contentPane.add(button2);
        button2.addActionListener((e) -> {
            UserLoginJFrame u = new UserLoginJFrame();
            u.add(new UserLoginJPanel());
            u.setVisible(true);
            this.dispose();
        });
        button2.setBounds(270, 245, 78, 30);
        /**
         * 生成4个随机数
         */
        for (int i = 0; i < number.length; i++) {
            number[i] = String.valueOf(random.nextInt(10));
            str = str + number[i];
        }
        System.out.println(str);
        //---- label5 ----
        label5.setText(number[0]);
        label5.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 14));
        label5.setForeground(Color.cyan);
        contentPane.add(label5);
        label5.setBounds(360, 185, 20, 20);

        //---- label6 ----
        label6.setText(number[1]);
        label6.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 14));
        label6.setIcon(null);
        label6.setForeground(new Color(255, 51, 51));
        contentPane.add(label6);
        label6.setBounds(385, 190, 20, label6.getPreferredSize().height);

        //---- label7 ----
        label7.setText(number[2]);
        label7.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 14));
        label7.setForeground(Color.yellow);
        contentPane.add(label7);
        label7.setBounds(410, 185, 15, label7.getPreferredSize().height);

        //---- label8 ----
        label8.setText(number[3]);
        label8.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 14));
        label8.setForeground(new Color(255, 51, 255));
        contentPane.add(label8);
        label8.setBounds(435, 190, 30, label8.getPreferredSize().height);

        { // compute preferred size
            Dimension preferredSize = new Dimension();
            for (int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        setSize(575, 430);
        setLocationRelativeTo(null);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    /**
     * 注册按钮事件处理函数
     */
    public void submit() throws SQLException {
        String newname = textField1.getText();
        String newp = textField2.getText();
        //获取输入的验证码
        newrandom = textField3.getText();
        System.out.println("newpassword:" + newrandom);
        if (newname.equals("") || newp.equals("") || newrandom.equals("")) {
            JOptionPane.showMessageDialog(this, "输入框不能为空值", "null", JOptionPane.WARNING_MESSAGE);
        } else {
            if (newrandom.equals(str)) {
                String selectsql = "select * from users_table where name=?";
                preparedStatement = con.prepareStatement(selectsql);
                preparedStatement.setString(1, newname);
                resultSet = preparedStatement.executeQuery();
                if (resultSet.next()) {
                    JOptionPane.showMessageDialog(this, "该账号已经存在", "have", JOptionPane.WARNING_MESSAGE);
                    textField1.setText("");
                } else {
                    String sql = "insert into users_table(name,password) values(?,?)";
                    preparedStatement = con.prepareStatement(sql);
                    preparedStatement.setString(1, newname);
                    preparedStatement.setString(2, newp);
                    result = preparedStatement.executeUpdate();
                    if (result > 0) {
                        JOptionPane.showMessageDialog(this, "注册成功", "success", JOptionPane.WARNING_MESSAGE);
                        User user = new User(newname, newp);
                        user.setName(newname);
                        user.setPassword(newp);
                        this.dispose();
                        UserLoginJFrame u = new UserLoginJFrame();
                        u.add(new UserLoginJPanel());
                        u.setVisible(true);
                        JDBCUtil.close(preparedStatement, resultSet, con);
                    }
                }
            } else {
                JOptionPane.showMessageDialog(this, "输入验证码错误", "fail", JOptionPane.ERROR_MESSAGE);

            }
        }
    }

    public static void main(String[] args) {
        new NewUserJFrame();
    }
}
